$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'module/blogarticle/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '封面', name: 'articleCover', index: 'article_cover', width: 100, formatter: function(value, options, row){
                return "<img src=\""+value+"\" style='width: 80px;height: 40px'/>";
            }},
			{ label: '标题', name: 'articleTitle', index: 'article_title', width: 300 },
			{ label: '作者', name: 'articleAuthor', index: 'article_author', width: 130 },
			{ label: '排序', name: 'articleSort', index: 'article_sort', width: 50 },
			{ label: '浏览次数', name: 'articleVisit', index: 'article_visit', width: 80 },
			{ label: '状态', name: 'articleStatus', index: 'article_status', width: 50 , formatter: function(value, options, row){
                return value === 1 ?
                    '<span class="label label-success">发布</span>' :
                    '<span class="label label-danger">待发布</span>';
            }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
        items:[],
        selecte:[{text:'发布',value:'1'},{text:'待发布',value:'2'}],
		blogArticle: {}
	},
    created: function(){
        $.get(baseURL + "module/blogarticlecate/lists", function(r){
            vm.items = r.blogarticlecate;
        });
    },
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){

			vm.showList = false;
			vm.title = "新增";
			vm.blogArticle = {};
            $('.summernote').summernote('code', '');
            vm.blogArticle.articleCover='http://bpic.588ku.com/element_origin_min_pic/01/47/02/12574338a640011.jpg';
            $.get(baseURL + "module/blogarticlecate/lists", function(r){
                vm.items = r.page;
            });
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            var html = $('.summernote').summernote('code');
            vm.blogArticle.articleContent=html;
            var images=$("#imgurl")[0].src;
            vm.blogArticle.articleCover=images;
            if (event==1){
            	vm.blogArticle.articleStatus=1;
			}
			else {
                vm.blogArticle.articleStatus=2;
			}

            if(vm.validator()){
                return ;
            }

            var url = vm.blogArticle.id == null ? "module/blogarticle/save" : "module/blogarticle/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.blogArticle),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "module/blogarticle/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "module/blogarticle/info/"+id, function(r){
                vm.blogArticle = r.blogArticle;
                $('.summernote').summernote('code', vm.blogArticle.articleContent);
            });
            $.get(baseURL + "module/blogarticlecate/lists", function(r){
                vm.items = r.page;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
        validator: function () {
            if(isBlank(vm.blogArticle.articleTitle)){
                alert("标题不能为空");
                return true;
            }
            if(isBlank(vm.blogArticle.articleAuthor)){
                alert("作者不能为空");
                return true;
            }
            if(isBlank(vm.blogArticle.articleCover)){
                alert("封面不能为空");
                return true;
            }
        }
	}
});